#include "common.h"
#include "searchdialog.h"

SearchDialog::SearchDialog ( QWidget * parent )
: QDialog ( parent )
, findLabel   ( this )
, leFind      ( this )
, listView    ( this )
, resultLabel ( this )
{
	setModal ( true );
	setWindowTitle ( "Search" );

	findLabel.setText ( "Find what:" );
	resultLabel.setText ( "Results: " );
	findLabel.setGeometry ( 10, 10, 50, 20 );
	leFind.setGeometry ( 65, 10, 100, 20 );
	listView.setGeometry ( 10, 30, 100, 100 );
	setGeometry ( 300, 300, 300, 200 );

	connect ( &leFind, SIGNAL ( returnPressed ()), this, SLOT ( onStrEntered ()));

	model = new QStringListModel ( this );
	QStringList strList;
	model -> setStringList ( strList );
	listView.setModel ( model );
}

SearchDialog::~SearchDialog ()
{
}

void SearchDialog::resizeEvent ( QResizeEvent * e )
{
	const int spacing = 10;
	findLabel.setGeometry ( 10, 10, 50, 20 );
	leFind.setGeometry ( findLabel.x() + findLabel.width() + spacing, findLabel.y(), 
		width() - 3 * spacing - findLabel.width(), findLabel.height ());

	resultLabel.setGeometry ( findLabel.x(), findLabel.y() + findLabel.height() + spacing,
		fontMetrics().width ( resultLabel.text()), fontMetrics().height());

	listView.setGeometry ( findLabel.x(), resultLabel.y() + resultLabel.height() + spacing,
		width() - 2 * spacing, height() - 4 * spacing - 2 * fontMetrics().height ());
}

void SearchDialog::displaySearchResults ( vector<MapObject *> results )
{
	QStringList strList;
	for ( size_t i = 0; i < results.size(); ++i )
	{
		std::string str;
		results[i] -> getProperty ( e_Label, str );
		strList.append ( QString ( str.data ()));
	}
	model -> setStringList ( strList );
}

void SearchDialog::onStrEntered ()
{
	emit wantToFind ( leFind.text ());
}